const Subscription = require('egg').Subscription;

class AssignTriggerTimes extends Subscription {
  // 每1分钟执行一次
  static get schedule() {
    return {
      interval: '13s',   // 1分钟
      type: 'worker',   // 只在某一个 worker 上执行
    };
  }

  async subscribe() {
    const { ctx } = this;
    try {
      const result = await ctx.service.phonedb.assignTriggerTimesForAllOwners();
      const resultsend = await ctx.service.sendmsgtask.assignTriggerTimesForAllOwners();
      console.log('✅ 定时分配计划  执行成功:', result.message,resultsend.message);
    } catch (error) {
      ctx.logger.error('❌ 定时分配计划  执行失败:', error.message);
    }
  }
}

module.exports = AssignTriggerTimes;